<?php
/**
 * Product Management
 */
class Admin_ProductController extends Zend_Controller_Action{
    function init(){
        System_Template::using('getit','Admin');
        // get parameter in request URL
        $this->Params = $this->getRequest()->getParams();
        $this->view->maintitle = "Product Management";
        $this->model = new Admin_Model_Product();
        $writer = new Zend_Log_Writer_Stream(APPLICATION_PATH.'/log.txt');
	$this->logger = new Zend_Log($writer);	
        parent::init();
    }
    /**
     * List product
     */
    function indexAction(){
        $storeId = 1;
        if($this->_request->getParam("STOREID") != null){
            $storeId = $this->_request->getParam("STOREID");
        }
        
        if(isset($this->Params['limit'])){
            $limit = (int)$this->Params['limit'];
        }else{
            $limit = 10;
        }
        if(isset($this->Params['page'])){
            $page = (int)$this->Params['page'];
        }else{
            $page = 1;
        }
        $numberOfItem = $this->model->count($storeId);
        $data = $this->model->fetchAllWithCategoryName($storeId,$limit,($page-1)*$limit);
        // Doan nay dam bao phan trang dung khi xoa
        if($page > 1 && sizeof($data) == 0){
            $page = $page - 1;
            $data = $this->model->fetchAllWithCategoryName($storeId,$limit,($page-1)*$limit);
        }
        $this->view->data = $data;
        $this->view->limit = $limit;
        $this->view->page = $page;
        $this->view->pagination = common::pagination("/admin/product/index",$numberOfItem,'right', $limit, $page);
    }
    
    /**
     *@author uyen.do
     *INVISIBLE
     *Redirect to add or edit form
     **/
    function inputAction(){
        $this->pcmodel = new Admin_Model_Productcategories();
        $storeId = 1;
        if($this->_request->getParam('STOREID')!=null){
            $storeId = $this->_request->getParam('STOREID');
        }
        $listCategories = $this->pcmodel->findByStoreId($storeId);
        $id = (isset($this->Params['id']) ? $this->Params['id'] : 0);
        if($id > 0){
            $data = $this->model->find($id)->current();
            $this->view->data = $data;
            $this->view->id = $id;
        }
        $this->view->listCategories = $listCategories;
    }
    
    /**
     *INVISIBLE
     *Save a new Product or edit an existed Product
     *
     **/
    
    function saveAction(){
        if($this->Params['PRODUCTID'] == ""){
            $this->model->addNewProduct($this->Params);
        }else{
            $this->model->updateProduct($this->Params);
        }
        
        // Luu lai phan trang truoc
        if(!isset($this->Params['limit'])){
            $this->_redirect('/admin/product/index');
        }else{
            $this->_redirect('/admin/product/index/limit/'.$this->Params['limit']."/page/".$this->Params['page']);
        }
    }
    
    /**
     *@author uyen.do
     *INVISIBLE
     *Delete an Product
     *
     **/
    
    function deleteAction(){
        $id = $this->Params['id'];
        $db = $this->model->getDefaultAdapter();
        $db->delete('product',"PRODUCTID = ".(int)$id);
        // Luu lai phan trang truoc
        if(!isset($this->Params['limit'])){
            $this->_redirect('/admin/product/index');
        }else{
            $this->_redirect('/admin/product/index/limit/'.$this->Params['limit']."/page/".$this->Params['page']);
        }
    }
    
    /**
     *
     *@author uyen.do
     *delete selected Products
     *
     **/
    
    function deletemultiproductsAction(){
        $ids = $this->Params['ITEM'];
        $this->logger->info(sizeof($ids));
        $db = $this->model->getDefaultAdapter();
        foreach($ids as $id){
            $db->delete('product', 'PRODUCTID = '. (int)$id);
        }
        // Luu lai phan trang truoc
        if(!isset($this->Params['limit'])){
            $this->_redirect('/admin/product/index');
        }else{
            $this->_redirect('/admin/product/index/limit/'.$this->Params['limit']."/page/".$this->Params['page']);
        }
    }
    /**
     *INVISIBLE
     *Upload action
     **/
    public function uploadAction(){
        $this->_helper->layout->disableLayout();
        try{
            $uploader = new qqFileUploader(array(), ini_get('post_max_size'));
	    $result = $uploader->handleUpload(PROJECT_HOME ."/upload/product/",true);
	    $this->logger->info($result['success']);
        }catch(Zend_Exception $e){
            $this->logger->info($e->getMessage());
        }
        echo Zend_Json::encode($result);
	exit;
    }
}
?>